// pages/coachInfo/coachInfo.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: {},
    staticUrl: '',
    apiUrl: '',
    imgUrl: 'http://static.sanfeng.maiworld.net/image/my/avatar3x.png',
    name:'',
    starIndex:4,
    sex:['男','女'],
    sexindex:0,
    age: null,
    coachAge:null,
    adoptNum:85,
    vehicleType:'捷达',
    licensePlate:'',
    phone:null,
    array: ['中国驾校', '美国驾校', '巴西驾校', '日本小驾校'],
    index: 0,
  },

  submitToAdmin:function(){
 // 发起网络请求
    var userinfo = wx.getStorageSync("userInfo");
    wx.request({
      url: this.data.apiUrl + '/coach/updateCoachInfo.do',
      data: {
        session_key: wx.getStorageSync("session_key"),
        openid: wx.getStorageSync("openid"),
        cid: userinfo.cid,
        sex: this.data.sexindex,
        phone: this.data.phone,
        plate_number: this.data.licensePlate,
        nickname: this.data.name,
        head_pic: this.data.imgUrl,
        drive_year: this.data.age,
        coach_year: this.data.coachAge,
        precent: this.data.adoptNum,
        star: userinfo.star
      },
      method: 'POST',
      success: (res) => {
        console.log(res, '修改教练基础信息')
        wx.setStorageSync("userInfo", res.data.userinfo);
        wx.showToast({
          title: '保存成功',
          icon:"success",
          duration:1500
        })
        setTimeout(()=>{
          wx.navigateBack({
            delta:1
          })
        },1500)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setData({
      baseUrl: app.globalData,
      staticUrl: app.globalData.staticUrl,
      apiUrl: app.globalData.apiUrl
    })

    var userinfo = wx.getStorageSync("userInfo");

    this.setData({
      imgUrl: userinfo.headPic,
      name: userinfo.nickname,
      phone: userinfo.phone,
      starIndex: userinfo.star,
      sexindex: userinfo.sex,
      age: userinfo.driveYear,
      coachAge: userinfo.coachYear,
      adoptNum: userinfo.precent,
      licensePlate: userinfo.plateNumber,
    })

  
  },
  uploadImg() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths;
        console.log(res);
        that.setData({
          imgUrl: tempFilePaths[0]
        })
        wx.uploadFile({
          url: that.data.apiUrl + '/v1/examination/upload.do',
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'content-type': 'application/json' // 默认值
          },
          success: function (res) {
            console.log(res, '上传图片路径');

            var result = JSON.parse(res.data);
            that.setData({
              imgUrl: result.result
            })
          }
        })
      }
    })
  },
  inputname:function(e){
    this.setData({
      name: e.detail.value
    })
  },
  bindSexChange: function (e) {
    this.setData({
      sexindex: e.detail.value
    })
  },
  inputage: function (e) {
    this.setData({
      age: e.detail.value
    })
  },
  inputcoachAge: function (e) {
    this.setData({
      coachAge: e.detail.value
    })
  },
  inputadoptNum: function (e) {
    this.setData({
      adoptNum: e.detail.value
    })
  },
  inputvehicleType: function (e) {
    this.setData({
      vehicleType: e.detail.value
    })
  },
  inputlicensePlate: function (e) {
    this.setData({
      licensePlate: e.detail.value
    })
  },
  inputphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})